#include "Ball.h"
#include "Level.h"
#include "Mob.h"
#include "FleshTile.h"


Ball::Ball()
{

}

void Ball::init(const sf::Vector2f position, const float moveX, const float moveY)
{
    m_dead = false;
    m_skin.setSize(sf::Vector2f(2, 2));
    m_skin.setFillColor(sf::Color::Blue);
    setPosition(position);
    m_moveX = moveX;
    m_moveY = moveY;
    m_inAir.restart();
    if(m_moveX > 0)
        m_direction = 2;
    else
        m_direction = 1;
}

void Ball::setPosition(const sf::Vector2f position)
{
    m_skin.setPosition(position);
}

sf::Vector2f Ball::getPosition() const
{
    return m_skin.getPosition();
}

bool Ball::getDead() const
{
    return m_dead;
}

void Ball::maj(Level *level, std::vector<Mob*> *mob)
{
    if(!m_dead)
    {
        m_skin.move(2 * m_moveX, 2 * m_moveY);
        if(m_direction == 1)
        {
            if(m_moveX < 0)
                m_moveX += 0.0015;
            if(m_moveX > 0)
                m_moveX = 0;
        }
        else
        {
            if(m_moveX > 0)
                m_moveX -= 0.0015;
            if(m_moveX < 0)
                m_moveX = 0;
        }
        if(m_moveY > -1)
            m_moveY+=0.008;

        if(level->entityInTile(getPosition(), 1, "InfectedFlesh", sf::Vector2f(2, 2)))
        {
            level->setTile(new FleshTile, (getPosition().x + 1) / 32, (getPosition().y + 2) / 32, 1);
            m_dead = true;
        }
        if(m_inAir.getElapsedTime().asSeconds() > 1.5)
            m_dead = true;
        if(level->entityInTile(getPosition(), 1, "Flesh", sf::Vector2f(2, 2)))
            m_dead = true;

        int xMob;
        int yMob;
        unsigned int e;
        for(e = 0; e < mob->size(); e++)
        {
            if(mob->at(e) != NULL)
            {
                xMob = mob->at(e)->getPosition().x;
                yMob = mob->at(e)->getPosition().y;
                if(getPosition().x + 2 > xMob && getPosition().x < xMob + 32 && getPosition().y + 2 > yMob && getPosition().y < yMob + 32)
                {
                    mob->at(e)->setDommage(1);
                    m_dead = true;
                }
            }
        }
    }
}

void Ball::render(sf::RenderWindow *app)
{
    if(!m_dead)
    {
        app->draw(m_skin);
    }
}
